/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classcreator.classfillers;

import classcreator.abstractclasses.AbstractClass;
import classcreator.abstractclasses.AbstractProperty;
import classcreator.abstractclasses.EnumDataType;
import java.util.ArrayList;

/**
 *
 * @author Samus
 */
public class Properties {

    private static Properties instance = new Properties();

    public static Properties getInstance() {
        return instance;
    }

    private Properties() {
    }

    public void assembleProperties(AbstractClass clase) {
        clase.setAssembledClass(addProperties(clase.getAssembledClass(), clase.getProperties()));
    }

    public ArrayList<String> addProperties(ArrayList<String> clase, ArrayList<AbstractProperty> prop) {
        ArrayList<String> classs = clase;
        for (AbstractProperty p : prop) {
            classs.add(propCreate(p));
        }
        for (AbstractProperty p : prop) {
            for (String s : addProperty(p)) {
                classs.add(s);
            }
        }
        return classs;
    }

    public ArrayList<String> addProperty(AbstractProperty prop) {
        ArrayList<String> classs = new ArrayList<String>();
        classs.add("");
        for (String s : addGetter(prop)) {
            classs.add(s);
        }
        for (String s : addSetter(prop)) {
            classs.add(s);
        }
        return classs;
    }

    public ArrayList<String> addGetter(AbstractProperty prop) {
        ArrayList<String> getter = new ArrayList<String>();
        String res = "public " + propSort(prop.getType()) + "get" + prop.getName() + "(){";
        getter.add(res);
        res = "return " + prop.getName() + ";";
        getter.add(res);
        res = "}";
        getter.add(res);
        getter.add("");
        return getter;
    }

    public ArrayList<String> addSetter(AbstractProperty prop) {
        ArrayList<String> setter = new ArrayList<String>();
        String res = "public " + "set" + prop.getName() + "(" + propSort(prop.getType()) + "new" + prop.getName() + "){";
        setter.add(res);
        res = "this." + prop.getName() + " = " + "new" + prop.getName() + ";";
        setter.add(res);
        res = "}";
        setter.add(res);
        
        return setter;
    }

    public String propCreate(AbstractProperty prop) {
        String res;
        res = "private ";
        res += propSort(prop.getType());
        res += prop.getName();
        res += ";";
        return res;
    }

    public String propSort(EnumDataType type) {
        String res = "";
        if (type == EnumDataType.BOOLEAN) {
            res = "boolean ";
        } else if (type == EnumDataType.COLLECTION) {
            res = "List ";
        } else if (type == EnumDataType.INT) {
            res = "int ";
        } else {
            res = "String ";
        }
        return res;
    }
}
